package com.gehj.androidlibs.fragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.app.Fragment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

import com.gehj.alluitls.androidUtils.LogUtil;
import com.gehj.androidlibs.R;
import com.gehj.uhttp.GlobleURL;
import com.gehj.uhttp.OkhttpUtils;
import com.gehj.uhttp.Singleton;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ReadedFragment extends Fragment implements LifecycleOwner {
    private static final String TAG = "ReadedFragment";
    private View view;
    private TextView textView;
    LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    //IMPORTANT: 判空的必要性，适配器中往往要有一个context用来进行inflate view
                    //IMPORTANT:与布局无关的可以用appliaction的context
                    if (null == getActivity()) return;
                        LogUtil.e(TAG, getActivity().getLocalClassName());
                        String s = (String) msg.obj;
                        textView.setText(s);


                break;
            }
        }

    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(OkhttpUtils.getInstance(TAG));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_main_handler2, null);
        initview();
        setListviewData();
        return view;
    }

    /*@Override
    public void onHiddenChanged(boolean hidden) {

        super.onHiddenChanged(hidden);
        setListviewData();
    }*/

    private void setListviewData() {
        OkhttpUtils.get_data(GlobleURL.WechatList, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                Message message = Message.obtain();
                message.what = 0;
                message.obj = json;
                handler.sendMessage(message);
            }
        });
    }


    private void initview() {
        textView = (TextView) view.findViewById(R.id.text);
    }




    @Override
    public void onDetach() {
        super.onDetach();
        OkhttpUtils.cancelTagCall(TAG);
        handler.removeCallbacksAndMessages(null);
        LogUtil.e(TAG,"onDetach");

    }


    @NotNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }
}

